package com.github.tvbox.osc.util;

import android.text.TextUtils;
import android.text.format.Time;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

public class Utils {

    public static String[] weeks = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /**
     * 获取当前日期，包含星期几
     *
     * @return 日期字符串 xx月xx号 星期x
     * @author drowtram
     */
    public static String getStringData() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mMonth + "月" + mDay + "日\n" + "星期" + mWay;
    }

    /**
     * 获取当前时间
     *
     * @return 时间字符串 24小时制
     * @author drowtram
     */
    public static String getStringTime(String type) {
        Time t = new Time();
        t.setToNow(); // 取得系统时间。
        String hour = t.hour < 10 ? "0" + (t.hour) : t.hour + ""; // 默认24小时制
        String minute = t.minute < 10 ? "0" + (t.minute) : t.minute + "";
        return hour + type + minute;
    }

    /**
     * 格式化日期
     *
     * @param format
     * @return
     */
    public static String getStringDateTime(String format) {
        if (TextUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取今日日期
     *
     * @param format 日期格式(默认：yyyy-MM-dd)
     * @return
     */
    public static String getNowDate(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TextUtils.isEmpty(format) ? "yyyy-MM-dd" : format);
        return simpleDateFormat.format(new Date());
    }

    /**
     * 根据标准格式的时间字符串得到对应的Date对象,标准格式如下:"2005-03-23 13:34:56".
     *
     * @return 时间字符串对应的Date对象.
     */
    public static Date getDateFromStandardTime(String standardTime, String format) {
        try {
            if (TextUtils.isEmpty(standardTime)) {
                return new Date();
            }
            if (TextUtils.isEmpty(format)) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            return new SimpleDateFormat(format).parse(standardTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getNowTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(new Date());
    }

    public static String getNowDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return simpleDateFormat.format(new Date());
    }

    public static String getSpecifiedFormatTime(Date date, String format) {
        if (TextUtils.isEmpty(format))
            format = "yyyy-MM-dd HH:mm:ss";
        if (null == date)
            date = new Date();

        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.format(date);
    }

    /**
     * 计算当前日期是星期几
     *
     * @return
     */
    public static String getWeekStr() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        return weeks[week - 1];
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 截取字符串
     * @param temp
     * @param length
     * @return
     */
    public static String getSubstring(String temp, int length) {
        if (TextUtils.isEmpty(temp)) {
            return "";
        }
        return temp.substring(0, temp.length() >= length ? length : temp.length());
    }

    /**
     * 获取map 值
     * @param item
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getMapValue(Map item, String key, String defaultValue) {
        String keySet = defaultValue;
        if (item != null) {
            if (item.containsKey(key)) {
                Object obj = item.get(key);
                if (obj != null) {
                    keySet = String.valueOf(obj);
                }
            }
            return keySet;
        } else {
            return keySet;
        }
    }
}
